﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using System.Configuration;
using YY.Common;
using System.Web.Script.Serialization;

namespace WebApp
{
    public partial class OrderFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(OrderFrm), this);
        }

        [AjaxPro.AjaxMethod]
        public int GetCurrentUser()
        {
            try
            {
                return (Session["User"] as TB_User).id;
            }
            catch { }
            return 0;
        }


        [AjaxPro.AjaxMethod]
        public AjaxResult<int> SaveBase(string id, string eOrderDate, string eBizMan, string eAgent, string eClient, string eInvoiceHeader, string eContactInfo,
            string eCCode, string eStartPort, string eEndPort, string eImptAndExpt, string eCustomsDeadline, string eStowageMethod, string eMemo, bool? eHasTrailer,
            bool? eHasInvoice, bool? eIsCustoms, bool? eIsOcean)
        {
            bool update = !string.IsNullOrEmpty(id);
            AjaxResult<int> result = new AjaxResult<int>();
            result.Data = 0;
            result.Msg = null;
            try
            {
                TB_Order order = new TB_Order();
                if (update)
                {
                    order = this.BaseDataProxy.GetOrder(Convert.ToInt32(id));
                    if (order.isSubmit)
                    {
                        result.Msg = "已提交财务审核单据不能修改！";
                        return result;
                    }
                }

                WebCommon.EvalControlValueToData(order, "cCode", eCCode);
                WebCommon.EvalControlValueToData(order, "client", eClient);
                WebCommon.EvalControlValueToData(order, "contactInfo", eContactInfo);
                WebCommon.EvalControlValueToData(order, "customsDeadline", eCustomsDeadline);
                WebCommon.EvalControlValueToData(order, "hasInvoice", eHasInvoice);
                WebCommon.EvalControlValueToData(order, "hasTrailer", eHasTrailer);
                WebCommon.EvalControlValueToData(order, "imptAndExpt", eImptAndExpt);
                WebCommon.EvalControlValueToData(order, "invoiceHeader", eInvoiceHeader);
                order.isClosed = false;
                WebCommon.EvalControlValueToData(order, "isCustoms", eIsCustoms);
                WebCommon.EvalControlValueToData(order, "isOcean", eIsOcean);
                WebCommon.EvalControlValueToData(order, "memo", eMemo);
                WebCommon.EvalControlValueToData(order, "operator", eBizMan);
                WebCommon.EvalControlValueToData(order, "orderTime", eOrderDate);
                WebCommon.EvalControlValueToData(order, "startPort", eStartPort);
                WebCommon.EvalControlValueToData(order, "endPort", eEndPort);
                WebCommon.EvalControlValueToData(order, "stowageMethod", eStowageMethod);

                if (update)
                {
                    this.BaseDataProxy.UpdateOrder(order);
                    result.Data = order.id;
                }
                else
                {
                    order.agent = (Session["User"] as TB_User).id;
                    string header = ConfigurationManager.AppSettings["OrderCodeHeader"];
                    int len = Convert.ToInt32(ConfigurationManager.AppSettings["SerialLen"]);
                    order.code = this.BaseDataProxy.GetSerialNo(1, header, len, true, true, true);
                    order.createTime = DateTime.Now;
                    //order.status = EnumCommon.OrderStatus.订舱录入.ToString();
                    int newId = this.BaseDataProxy.AddOrder(order);
                    result.Data = newId;
                }
            }
            catch (Exception ex)
            {
                result.Data = 0;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> SaveTC(int orderId, string agent, string deadline, string address, bool isPonderation, string memo, string opContact, string charge, string returnPort, string specs, string stowageArea, string stowageContact)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;

            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                TB_Trailer trailer = new TB_Trailer();
                WebCommon.EvalControlValueToData(trailer, "address", address);
                WebCommon.EvalControlValueToData(trailer, "agent", agent);
                WebCommon.EvalControlValueToData(trailer, "deadline", deadline);
                WebCommon.EvalControlValueToData(trailer, "isPonderation", isPonderation);
                WebCommon.EvalControlValueToData(trailer, "memo", memo);
                WebCommon.EvalControlValueToData(trailer, "opContact", opContact);
                WebCommon.EvalControlValueToData(trailer, "charge", charge);
                WebCommon.EvalControlValueToData(trailer, "returnPort", returnPort);
                WebCommon.EvalControlValueToData(trailer, "specs", specs);
                WebCommon.EvalControlValueToData(trailer, "stowageArea", stowageArea);
                WebCommon.EvalControlValueToData(trailer, "stowageContact", stowageContact);

                result.Data = this.BaseDataProxy.UpdateTrailer(orderId, new TB_Trailer[] { trailer });

            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;


        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Trailer> GetTC(int orderId)
        {
            AjaxResult<TB_Trailer> result = new AjaxResult<TB_Trailer>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetTrailer(orderId);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> ClearTC(int orderId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                result.Data = this.BaseDataProxy.UpdateTrailer(orderId, null);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> PrintWTS(string code, string cCode, int orderId, string tcClientName, string stowageContact, string deadLineTime, string address, string specs, string charge, string memo, string opId)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            //ExcelCommon excel = new ExcelCommon();
            //try
            //{
            //    TB_Trailer trailer = this.BaseDataProxy.GetTrailer(orderId);
            //    if (trailer == null)
            //        return result;

            //    excel.Create();
            //    excel.ws = excel.AddSheet("sh1");
            //    excel.app.Visible = true;

            //    excel.SetCellValue(excel.ws, 1, 1, "广州市盈洋货运代理有限公司");///row1
            //    //excel.UniteCells(excel.ws, 1, 1, 1, 1);
            //    excel.SetCellProperty(excel.ws, 1, 1, 1, 1, 30, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false);

            //    excel.SetCellValue(excel.ws, 3, 1, "派车委托书");//row3
            //    //excel.UniteCells(excel.ws, 3, 1, 3, 1);
            //    excel.SetCellProperty(excel.ws, 3, 1, 3, 1, 20, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false);

            //    //TB_Clients c = this.clients.Single(p => p.id == this._Order.client);
            //    excel.SetCellValue(excel.ws, 4, 1, "致" + tcClientName + " " + stowageContact);//row4
            //    //excel.UniteCells(excel.ws, 4, 1, 4, 5);
            //    //excel.SetCellProperty(excel.ws, 4, 1, 4, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);                

            //    excel.SetCellValue(excel.ws, 5, 1, "兹委托贵公司派车到以下地点装货，资料如下：");//row5
            //    //excel.UniteCells(excel.ws, 5, 1, 5, 5);
            //    //excel.SetCellProperty(excel.ws, 5, 1, 5, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);                

            //    excel.SetCellValue(excel.ws, 6, 1, "1、订舱号：" + cCode);//row6
            //    //excel.UniteCells(excel.ws, 6, 1, 6, 5);
            //    //excel.SetCellProperty(excel.ws, 6, 1, 6, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);                

            //    excel.SetCellValue(excel.ws, 7, 1, "到厂装货时间：" + deadLineTime);//row7
            //    //excel.UniteCells(excel.ws, 7, 1, 7, 5);
            //    //excel.SetCellProperty(excel.ws, 7, 1, 7, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);                

            //    excel.SetCellValue(excel.ws, 8, 1, "2、装货地点：" + address);//row8
            //    //excel.UniteCells(excel.ws, 8, 1, 8, 5);
            //    //excel.SetCellProperty(excel.ws, 8, 1, 8, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, true);                

            //    excel.SetCellValue(excel.ws, 9, 1, "3、联系人：" + stowageContact);//row9
            //    //excel.UniteCells(excel.ws, 9, 1, 9, 5);
            //    //excel.SetCellProperty(excel.ws, 9, 1, 9, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, true);                

            //    excel.SetCellValue(excel.ws, 10, 1, "4、柜型：" + specs);//row10
            //    //excel.UniteCells(excel.ws, 10, 1, 10, 10);
            //    //excel.SetCellProperty(excel.ws, 10, 1, 10, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, true);

            //    excel.SetCellValue(excel.ws, 11, 1, "5、运费：" + charge);//row11
            //    //excel.UniteCells(excel.ws, 11, 1, 11, 10);
            //    //excel.SetCellProperty(excel.ws, 11, 1, 11, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, true);

            //    excel.SetCellValue(excel.ws, 12, 1, "6、备注：" + memo);//row12
            //    //excel.UniteCells(excel.ws, 12, 1, 12, 10);
            //    //excel.SetCellProperty(excel.ws, 12, 1, 12, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, true);

            //    excel.SetCellValue(excel.ws, 13, 1, "7、（请准时到达。柜号司机资料麻烦到时发到15018476677 谢谢！）");//row13
            //    //excel.UniteCells(excel.ws, 13, 1, 13, 10);
            //    //excel.SetCellProperty(excel.ws, 13, 1, 13, 5, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, true);

            //    var opName=string.Empty;
            //    var opTel = string.Empty;
            //    if (!string.IsNullOrEmpty(opId))
            //    {
            //        TB_User op = this.BaseDataProxy.GetUser(Convert.ToInt32(opId));
            //        opName = op.userName;
            //        op.tel = opTel;
            //    }
            //    excel.SetCellValue(excel.ws, 15, 1, "操作人员：" + opName);//row4 col6
            //    //excel.UniteCells(excel.ws, 4, 7, 4, 10);
            //    //excel.SetCellProperty(excel.ws, 4, 7, 4, 10, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);

            //    excel.SetCellValue(excel.ws, 16, 1, "我司单号：" + code);//row5 col6
            //    //excel.UniteCells(excel.ws, 5, 7, 5, 10);
            //    //excel.SetCellProperty(excel.ws, 5, 7, 5, 10, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);

            //    excel.SetCellValue(excel.ws, 17, 1, "座机号：020-28945185");//row6 col6
            //    //excel.UniteCells(excel.ws, 6, 7, 6, 10);
            //    //excel.SetCellProperty(excel.ws, 6, 7, 6, 10, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);

            //    excel.SetCellValue(excel.ws, 18, 1, "手机：" + opTel);//row7 col6
            //    //excel.UniteCells(excel.ws, 7, 7, 7, 10);
            //    //excel.SetCellProperty(excel.ws, 7, 7, 7, 10, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);

            //    excel.SetCellValue(excel.ws, 19, 1, "传真号：020-32372253");//row8 col6
            //    //excel.UniteCells(excel.ws, 8, 7, 8, 10);
            //    //excel.SetCellProperty(excel.ws, 8, 7, 8, 10, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);

            //    excel.SetCellValue(excel.ws, 20, 1, "日期：" + DateTime.Now.ToString("yyyy-MM-dd"));//row9 col6
            //    //excel.UniteCells(excel.ws, 9, 7, 9, 10);
            //    //excel.SetCellProperty(excel.ws, 9, 7, 9, 10, 12, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);

            //    excel.SetCellProperty(excel.ws, 4, 1, 20, 1, 15, true, "宋体", Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlLeft, false);
            //    excel.ColumnsAutoFit(excel.ws, 1, 1, 20, 1);
            //    string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            //    string filePath = Server.MapPath("~/" + fileName);
            //    excel.SaveAs(filePath);
            //}
            //catch (Exception ex)
            //{
            //    result.Data = false;
            //    result.Msg = ex.Message;
            //    excel.Dispose();
            //}
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> CancelOrder(int id)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                MsgBoolean mb = this.BaseDataProxy.DeleteOrder(id);
                result.Data = mb.Ok;
                result.Msg = mb.Msg;
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Order> GetOrderBase(int id)
        {
            AjaxResult<TB_Order> result = new AjaxResult<TB_Order>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetOrder(id);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> SaveBG(int orderId, bool isCheck, bool isDrawback, bool isKdsk, bool isNotice, bool isOffer, bool isPass, string memo, string notifier, string receiver,
            string runNum, string status, string stockholder, string transferType, string address, string agent, string applyDate, string boatCorp, string boatName, string bookingMemo,
            string passTime, string contactMan, string contactTel, string demand, string destination, string endPort, string checkOff)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;

            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                if (order.isSubmit)
                {
                    result.Msg = "已提交财务审核单据不能修改！";
                    return result;
                }

                //TB_Customs customs = new TB_Customs();
                //WebCommon.EvalControlValueToData(customs, "isCheck", isCheck);
                //WebCommon.EvalControlValueToData(customs, "isDrawback", isDrawback);
                //WebCommon.EvalControlValueToData(customs, "isKdsk", isKdsk);
                //WebCommon.EvalControlValueToData(customs, "isNotice", isNotice);
                //WebCommon.EvalControlValueToData(customs, "isOffer", isOffer);
                //WebCommon.EvalControlValueToData(customs, "isPass", isPass);
                //WebCommon.EvalControlValueToData(customs, "memo", memo);
                //WebCommon.EvalControlValueToData(customs, "notifier", notifier);
                //WebCommon.EvalControlValueToData(customs, "receiver", receiver);
                //WebCommon.EvalControlValueToData(customs, "runNum", runNum);
                //WebCommon.EvalControlValueToData(customs, "status", status);
                //WebCommon.EvalControlValueToData(customs, "stockholder", stockholder);
                //WebCommon.EvalControlValueToData(customs, "transferType", transferType);
                //WebCommon.EvalControlValueToData(customs, "address", address);
                //WebCommon.EvalControlValueToData(customs, "agent", agent);
                //WebCommon.EvalControlValueToData(customs, "applyDate", applyDate);
                //WebCommon.EvalControlValueToData(customs, "boatCorp", boatCorp);
                //WebCommon.EvalControlValueToData(customs, "boatName", boatName);
                //WebCommon.EvalControlValueToData(customs, "bookingMemo", bookingMemo);
                //WebCommon.EvalControlValueToData(customs, "passTime", passTime);
                //WebCommon.EvalControlValueToData(customs, "contactMan", contactMan);
                //WebCommon.EvalControlValueToData(customs, "contactTel", contactTel);
                //WebCommon.EvalControlValueToData(customs, "demand", demand);
                //WebCommon.EvalControlValueToData(customs, "destination", destination);
                //WebCommon.EvalControlValueToData(customs, "endPort", endPort);
                //WebCommon.EvalControlValueToData(customs, "contactMan", contactMan);
                //IEnumerable<TB_Workflow> wfs = new JavaScriptSerializer().Deserialize<IEnumerable<TB_Workflow>>(checkOff);
                IEnumerable<TB_Workflow> wfs = WebCommon.JsonDeserialize<IEnumerable<TB_Workflow>>(checkOff);
                var ok = wfs.Any();
                //if (checkOff != null & checkOff.Any())
                //    customs.TB_CheckOff = OTConverter.ConvertTableToObject<TB_CheckOff>(this._DTHX).ToArray();
                //result.Data = this.BaseDataProxy.UpdateCustoms(orderId, new TB_Customs[] { customs });

            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Customs> GetBG(int orderId)
        {

            AjaxResult<TB_Customs> result = new AjaxResult<TB_Customs>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetCustoms(orderId);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
